package class_wuziqi;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import java.util.Calendar;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Login extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Login lg;
	private Socket socket;
	private int port;
	public JTextField username_text;
	public JTextField ipAddress_text;
	public static Login getInstance(){
		return lg;
	}
	public Login() {
		lg=this;
		GridBagConstraints gbc=new GridBagConstraints();
		setOpaque(false);
		setLayout(new GridBagLayout());
		Font font =new Font("黑体", Font.BOLD, 20);
		JLabel jl1=new JLabel("用户名：");
		gbc.gridx=0;gbc.gridy=0;gbc.anchor=GridBagConstraints.EAST;
		jl1.setFont(font);
		jl1.setForeground(Color.white);
		add(jl1, gbc);
		
		JLabel jl2=new JLabel("对方IP地址：");
		gbc.gridx=0;gbc.gridy=1;
		jl2.setFont(font);
		jl2.setForeground(Color.white);
		add(jl2, gbc);
		
		username_text=new JTextField();//用户名txt
		gbc.gridx=1;gbc.gridy=0;gbc.gridwidth=2;gbc.fill=GridBagConstraints.BOTH;//填充
		gbc.insets=new Insets(10, 10, 10, 10);
		add(username_text, gbc);
		
		ipAddress_text=new JTextField();//IP地址txt
		gbc.gridx=1;gbc.gridy=1;
		add(ipAddress_text, gbc);
		
		gbc.insets=new Insets(0, 0, 0, 50);
		gbc.gridwidth=1;
		JButton login=new JButton("登录");
		login.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String username=username_text.getText();
				String ipAddress=ipAddress_text.getText();
				User user=new User();
				if(username==null||"".equals(username)){
					JOptionPane.showMessageDialog(null, "请输入用户名");
					return;
				}
				port=MainPro.getInstance().server.getLocalPort();
				Map<Integer, Integer> map=MainPro.getInstance().map;
				try {
					socket=new Socket(ipAddress, map.get(port));
					Calendar calendar=Calendar.getInstance();
					int hour=calendar.get(Calendar.HOUR_OF_DAY);
					int minute=calendar.get(Calendar.MINUTE);
					int second=calendar.get(Calendar.SECOND);
					user.setLogin_time(hour+":"+minute+":"+second);
					user.setName(username);
					user.setIp(ipAddress);
					MainPro.getInstance().sendMessage(socket, user);
					MainPro.getInstance().setUser(user, true);
					setVisible(false);
				} catch (Exception e2) {
					e2.printStackTrace();
					JOptionPane.showMessageDialog(null, "请输入正确的IP地址！");
					return;
				}
			}
		});
		gbc.gridx=1;gbc.gridy=2;gbc.insets=new Insets(0, 40, 10, 50);
		add(login, gbc);
		
		JButton cancel=new JButton("取消");
		gbc.gridx=2;gbc.gridy=2;
		add(cancel, gbc);
	}
	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g1=(Graphics2D)g;
		Composite composite=g1.getComposite();
		g1.setComposite(AlphaComposite.SrcOver.derive(0.5f));
		g1.setColor(new Color(0x000000));
		g1.fill3DRect(0, 0, getWidth(), getHeight(), true);
		g1.setComposite(composite);
		super.paintComponent(g1);
	}
	public Socket getSocket(){
		return socket;
	}
}
